public class Solution10 {
    public boolean isMatch(String s, String p) {
        boolean[][] f=new boolean[s.length()+1][p.length()+1];
        f[0][0]=true;
        for (int i=1;i<p.length();i+=2){
            if (p.charAt(i)=='*'){
                f[0][i+1]=true;
            }else{
                break;
            }
        }
        for (int i=1;i<=s.length();i++){
            for (int j=1;j<=p.length();j++){
                if (p.charAt(j-1)=='*'){
                    f[i][j]=f[i-1][j]&&(s.charAt(i-1)==p.charAt(j-2)||p.charAt(j-2)=='.')||f[i][j-2];
                }else if (p.charAt(j-1)=='.'){
                    f[i][j]=f[i-1][j-1];
                }else{
                    f[i][j]=f[i-1][j-1]&&p.charAt(j-1)==s.charAt(i-1);
                }
            }
        }
        return f[s.length()][p.length()];
    }

    public static void main(String[] args) {
        System.out.println(new Solution10().isMatch("aab","c*a*b"));
    }
}
